﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;



public class GlobalAnimator : MonoBehaviour
{
    private Dictionary<string,FSMAnimator> animatorStore = new Dictionary<string, FSMAnimator>();



    public void AddAnimatorInList<T>(string ID,GameObject target) where T:FSMAnimator,new()
    {
        var animator = target.GetComponent<Animator>();
        if(animator)
        {
            Debug.LogError("尝试把animator放入动画对象池中时，对象上并未挂载Animator组件！，ID为："+ID);
            return;
        }

        AddAnimatorInList<T>(ID,animator);
    }

    public void AddAnimatorInList<T>(string ID,Animator animator) where T:FSMAnimator,new()
    {
        if(animator==null)
        {
            Debug.LogError("尝试把Animator放入动画对象池时，animator为null");
            return;
        }

        var fsmAnimator = new T();
        fsmAnimator.SetAnimator(animator);

        animatorStore.Add(ID,fsmAnimator);
    }


    public void Play<T>(string ID,T status) where T:Enum
    {
        if(!animatorStore.ContainsKey(ID))
        {
            Debug.LogError("GlobleAnimator中并不存在指定ID,请检查ID:"+ID);
            return;
        }

        var select = animatorStore[ID];
        if(select == null)
        {
            Debug.LogError("调用选中的Animator时，animator已被销毁，ID："+ID);
            return;
        }

        select.Play(status);
    }


    
    private static GlobalAnimator instance;
    public static GlobalAnimator Instance
    {
        get
        {
            if(instance==null)
            {
                var instanceObj = new GameObject();
                instanceObj.name = "GlobalAnimator";
                instance = instanceObj.AddComponent<GlobalAnimator>();
            }

            return instance;
        }
    }
    
}
